package cn.nbt.busManage.model.dto;

import cn.nbt.busManage.model.entity.Role;
import cn.nbt.busManage.model.entity.User;
import cn.nbt.busManage.model.vo.RoleVo;
import cn.nbt.busManage.model.vo.UserVo;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
public class UserDto {
    private Integer id;
    private String userName;
    private List<RoleDto> roles;
    private String password;
    private String email;
    private String phone;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    public UserDto(User user){
        this.id = user.getId();
        this.userName = user.getUserName();
        this.updateTime = user.getUpdateTime();
        this.email = user.getEmail();
        this.phone = user.getPhone();
        this.password = user.getPassword();
        if (user.getRoles()==null){
            this.roles = null;
        }else {
            List<Role> roleList = user.getRoles();
            List<RoleDto> roles = new ArrayList<>();
            for (Role role : roleList) {
                RoleDto roleDto = new RoleDto(role);
                roles.add(roleDto);
            }
            this.roles = roles;
        }
        this.createTime = user.getCreateTime();

    }

    public UserDto(UserVo userVo) {
        this.id = userVo.getId();
        this.userName = userVo.getUserName();
        this.password = userVo.getPassword();
        this.email = userVo.getEmail();
        this.phone = userVo.getPhone();
        this.createTime = userVo.getCreateTime();
        this.updateTime = userVo.getUpdateTime();
        List<RoleVo> roleVos = userVo.getRoles();
        if (roleVos != null) {
            ArrayList<RoleDto> roleDtos = new ArrayList<>();
            for (RoleVo roleVo : roleVos) {
                RoleDto roleDto = new RoleDto(roleVo);
                roleDtos.add(roleDto);
            }
            this.roles = roleDtos;
        }
    }
}
